{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 19932,
     "status": "ok",
     "timestamp": 1651041985150,
     "user": {
      "displayName": "YING XU",
      "userId": "11756690436182978895"
     },
     "user_tz": -480
    },
    "id": "wjheRYhdufgu",
    "outputId": "a42ee52b-f49f-46d5-b128-6ef23ccaed2d"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Mounted at /content/drive\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "from google.colab import drive\n",
    "drive.mount('/content/drive')\n",
    "sys.path.append('/content/drive/MyDrive/Colab Notebooks/tcm embedding')\n",
    "sys.argv=['']\n",
    "del sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "executionInfo": {
     "elapsed": 21106,
     "status": "ok",
     "timestamp": 1651042006240,
     "user": {
      "displayName": "YING XU",
      "userId": "11756690436182978895"
     },
     "user_tz": -480
    },
    "id": "sMaHUBlYucBR"
   },
   "outputs": [],
   "source": [
    "with open(\"/content/drive/MyDrive/Colab Notebooks/tcm embedding/seg_tcm.txt\",encoding=\"utf-8\") as f:\n",
    "    corpus = f.read()\n",
    "\n",
    "corpus_ls = corpus.split(\"\\n\")\n",
    "\n",
    "corpus_lst = []\n",
    "for sen in corpus_ls:\n",
    "    sen_ls = sen.split(\" \")\n",
    "    corpus_lst.append(sen_ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 6,
     "status": "ok",
     "timestamp": 1651042006241,
     "user": {
      "displayName": "YING XU",
      "userId": "11756690436182978895"
     },
     "user_tz": -480
    },
    "id": "fOCVZ-epucBT",
    "outputId": "22df61ce-01d4-447a-e2ba-83587f78d8a5"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3185574"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(corpus_lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "executionInfo": {
     "elapsed": 572,
     "status": "ok",
     "timestamp": 1651042006810,
     "user": {
      "displayName": "YING XU",
      "userId": "11756690436182978895"
     },
     "user_tz": -480
    },
    "id": "eYqVx4ESucBA"
   },
   "outputs": [],
   "source": [
    "from gensim.models.word2vec import Word2Vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "executionInfo": {
     "elapsed": 438586,
     "status": "ok",
     "timestamp": 1651042445390,
     "user": {
      "displayName": "YING XU",
      "userId": "11756690436182978895"
     },
     "user_tz": -480
    },
    "id": "L3IaR_RuucBV",
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "model = Word2Vec(corpus_lst,min_count=5,size=200,workers=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "executionInfo": {
     "elapsed": 390,
     "status": "ok",
     "timestamp": 1651042659193,
     "user": {
      "displayName": "YING XU",
      "userId": "11756690436182978895"
     },
     "user_tz": -480
    },
    "id": "O-_oiPECucBX"
   },
   "outputs": [],
   "source": [
    "model.save(\"./TCM_w2v\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "executionInfo": {
     "elapsed": 11,
     "status": "ok",
     "timestamp": 1651043705773,
     "user": {
      "displayName": "YING XU",
      "userId": "11756690436182978895"
     },
     "user_tz": -480
    },
    "id": "cCGbzkOk3DMA",
    "outputId": "a2023052-6da2-42c3-c7bf-f9e1a52b717f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "63896"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(model.wv.vocab)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "executionInfo": {
     "elapsed": 9659,
     "status": "ok",
     "timestamp": 1651044157292,
     "user": {
      "displayName": "YING XU",
      "userId": "11756690436182978895"
     },
     "user_tz": -480
    },
    "id": "kpqiEk5z4fql"
   },
   "outputs": [],
   "source": [
    "model.wv.save_word2vec_format('TCM_w2v.txt',binary=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "qIRmGLKs0zoV"
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "collapsed_sections": [],
   "name": "training.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
